package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"oa_kadia_link/utils"
	"time"
)

func GetToday() gin.HandlerFunc {
	return func(context *gin.Context) {
		transaction := context.MustGet("transaction").(*gorm.DB)
		reason := make(map[string]interface{})
		transaction.Table("records").
			Where("user_id = ? and date_time = ?", context.MustGet("userId"), time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli()))).
			Find(&reason)
		context.Set("json", reason)
	}
}

func PutToday() gin.HandlerFunc {
	return func(context *gin.Context) {
		var requestBody struct {
			Longitude *float64 `json:"longitude" binding:"required"`
			Latitude *float64 `json:"latitude" binding:"required"`
			Health *string `json:"health" binding:"required,oneof=正常 异常"`
			Remark *string `json:"remark"`
			Test *int `json:"test"`
		}
		utils.CheckRequestBody(context, &requestBody)
		transaction := context.MustGet("transaction").(*gorm.DB)
		utils.CheckError(transaction.Table("records").
			Create(map[string]interface{}{
				"user_id":context.MustGet("userId"),
				"date_time":time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli())),
				"health":requestBody.Health,
				"longitude":requestBody.Longitude,
				"latitude":requestBody.Latitude,
				"remark":nil,
		}).Error)
	}
}

func PutApply() gin.HandlerFunc {
	return func(context *gin.Context) {
		var requestBody struct {
			DateTime *int64 `json:"date_time" binding:"required"`
			Reason *string `json:"reason" binding:"required"`
		}
		utils.CheckRequestBody(context, &requestBody)
		transaction := context.MustGet("transaction").(*gorm.DB)
		var leaderId int64
		transaction.Table("users").
			Select("leader_id").
			Where("id = ?", context.MustGet("userId")).
			Find(&leaderId)
		utils.CheckError(transaction.Table("applies").
			Create(map[string]interface{}{
				"user_id":   context.MustGet("userId"),
				"date_time": time.UnixMilli(utils.GetTodayTime(*requestBody.DateTime)),
				"reason":    requestBody.Reason,
				"approve":   leaderId,
			}).Error)
	}
}

func GetAccess() gin.HandlerFunc {
	return func(context *gin.Context) {
		transaction := context.MustGet("transaction").(*gorm.DB)
		var count int64
		transaction.Table("records").
			Where("user_id = ? and health = '正常' and date_time between ? and ?", context.MustGet("userId"), time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli() - 14 * 24 * 60 * 60 *1000)), time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli()))).
			Count(&count)
		context.Set("json",count >= 14)
	}
}

func GetRecords() gin.HandlerFunc {
	return func(context *gin.Context) {
		transaction := context.MustGet("transaction").(*gorm.DB)
		reasons := make([]map[string]interface{}, 0)
		transaction.Table("records").
			Where("user_id = ? and date_time between ? and ?", context.MustGet("userId"), time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli() - 14 * 24 * 60 * 60 *1000)), time.UnixMilli(utils.GetTodayTime(time.Now().UnixMilli()))).
			Order("date_time desc").
			Find(&reasons)
		context.Set("json", reasons)
	}
}

func GetApplies() gin.HandlerFunc {
	return func(context *gin.Context) {
		transaction := context.MustGet("transaction").(*gorm.DB)
		reasons := make([]map[string]interface{}, 0)
		transaction.Table("applies").
			Joins("left join users on applies.approve = users.id ").
			Select("applies.id", "applies.approve", "users.position", "date_time", "reason").
			Where("user_id = ? ", context.MustGet("userId")).
			Order("applies.date_time desc").
			Find(&reasons)
		context.Set("json", reasons)
	}
}

func GetApproves() gin.HandlerFunc {
	return func(context *gin.Context) {
		transaction := context.MustGet("transaction").(*gorm.DB)
		reasons := make([]map[string]interface{}, 0)
		transaction.Table("applies").
			Joins("left join users on applies.user_id = users.id ").
			Select("applies.id", "users.name", "users.account", "date_time", "reason").
			Where("approve = ? ", context.MustGet("userId")).
			Find(&reasons)
		context.Set("json", reasons)
	}
}

func Approve() gin.HandlerFunc {
	return func(context *gin.Context) {
		var requestBody struct {
			ApproveId *int64 `json:"approve_id" binding:"required"`
			Allow *bool `json:"allow" binding:"required"`
		}
		utils.CheckRequestBody(context, &requestBody)
		transaction := context.MustGet("transaction").(*gorm.DB)
		if *requestBody.Allow == true{
			var tmp map[string]interface{}
			transaction.Table("users").
				Select("leader_id").
				Where("id = ?", context.MustGet("userId")).
				Find(&tmp)
			utils.CheckError(transaction.Table("applies").
				Where("id = ? and approve = ?",requestBody.ApproveId, context.MustGet("userId")).
				Updates(map[string]interface{}{
					"approve":tmp["leader_id"],
			}).Error)
		}else{
			utils.CheckError(transaction.Table("applies").
				Where("id = ? and approve = ?",requestBody.ApproveId, context.MustGet("userId")).
				Updates(map[string]interface{}{
					"approve":-1,
			}).Error)
		}
	}
}
